import fetch from 'dva/fetch';
// import { notification } from 'antd';

function checkStatus(response) {
  if (response.status ===200) {
      return response;
  }else{
      const error = new Error(response.statusText);
      error.name = response.status;
      error.response = response;
      throw error;
  }
}

/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
export default function request(url, options) {
  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  if (newOptions.method === 'POST' || newOptions.method === 'PUT' ||newOptions.method === 'DELETE') {
    newOptions.headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json; charset=utf-8',
      ...newOptions.headers,
    };
    newOptions.body = JSON.stringify(newOptions.body);
  }

  return fetch(url, newOptions)
    .then(checkStatus)
    .then(res => res.json())
    // .then(res => {
    //     if(res.code===200||res.code===402){
    //       return res;
    //     }else{
    //         const error = new Error(res.msg);
    //         error.name = res.code;
    //         error.body = res;
    //         throw error;
    //     }
    // })
}
